﻿using System;
using OverStore.Runtime.OverStoreContext;
using OverStore.Runtime.Storage;
using OverStore.Runtime.ObjectRepository.CommandSet;

namespace OverStore.Configuration.ObjectRepository.CommandSet
{    
    /// <summary>
    /// Configuration for <see cref="ICommandSet"/> component uses custom commands.
    /// </summary>
    public sealed class CommandSetConfiguration : IComponentConfiguration<ICommandSet>
    {
        /// <summary>
        /// Gets or sets the select command.
        /// </summary>
        public IComponentConfiguration<ICommand> SelectCommand { get; set; }

        /// <summary>
        /// Gets or sets the update command.
        /// </summary>
        public IComponentConfiguration<ICommand> UpdateCommand { get; set; }

        /// <summary>
        /// Gets or sets the insert command.
        /// </summary>
        public IComponentConfiguration<ICommand> InsertCommand { get; set; }

        /// <summary>
        /// Gets or sets the delete command.
        /// </summary>
        public IComponentConfiguration<ICommand> DeleteCommand { get; set; }

        /// <summary>
        /// Creates the command set.
        /// </summary>
        /// <param name="context">The current context.</param>
        /// <returns>The new command set instance.</returns>
        public ICommandSet CreateComponent(IOverStoreContext context)
        {
            if (Object.ReferenceEquals(context, null))
                throw new ArgumentNullException("context");

            return new OverStore.Runtime.ObjectRepository.CommandSet.CommandSet
                       {
                           SelectCommand = SelectCommand == null
                                               ? null
                                               : SelectCommand.CreateComponent(context),
                           UpdateCommand = UpdateCommand == null
                                               ? null
                                               : UpdateCommand.CreateComponent(context),
                           InsertCommand = InsertCommand == null
                                               ? null
                                               : InsertCommand.CreateComponent(context),
                           DeleteCommand = DeleteCommand == null
                                               ? null
                                               : DeleteCommand.CreateComponent(context)
                       };
        }
    }
}